package com.example.yingjiguanli_demo.controller;

import com.example.yingjiguanli_demo.aspect.interfaces.RateLimiter;
import com.example.yingjiguanli_demo.aspect.interfaces.RequirePermission;
import com.example.yingjiguanli_demo.pojo.LoginRequest;
import com.example.yingjiguanli_demo.pojo.Result;
import com.example.yingjiguanli_demo.pojo.UserRoleDTO;
import com.example.yingjiguanli_demo.service.UsersService;
import com.example.yingjiguanli_demo.utils.UserUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("api/v1/users")
public class UserController {
    @Resource
    private UsersService usersService;

    @Autowired
    private UserUtils userUtils;

    /***
     * 登录
     * @param loginRequest 登录信息
     * @param response response
     * @param request request
     * @return 登录结果
     */
    @ApiOperation(value = "登录模块")
    @PostMapping("/login")
    public Result<T> login(@RequestBody LoginRequest loginRequest, HttpServletResponse response, HttpServletRequest request) {
        return usersService.login(loginRequest, response,request);
    }

    /***
     * 注册
     * @param username 用户名
     * @param password 密码
     * @param email 邮箱
     * @return 注册结果
     */
    @ApiOperation(value = "注册模块")
    @PostMapping("/register")
    public Result<T> register(@RequestParam String username,@RequestParam String password,@RequestParam String email){
        return usersService.register(username, password,email);
    }

    /***
     * 用户登出
     * @param request 请求对象
     * @param response 响应对象
     * @return 返回登出结果
     */
    @ApiOperation(value = "退出模块")
    @RequirePermission("user")
    @RateLimiter
    @GetMapping("/outLogin")
    public Result<T> logout(HttpServletRequest request, HttpServletResponse response) {
        return usersService.logout(request, response);
    }

    /***
     * 获取所有用户
     * @return 获取所有用户结果
     */
    @ApiOperation(value = "获取所有用户")
    @GetMapping("/getAllUsers")
    @RequirePermission("admin")
    @RateLimiter
    public Result<List<UserRoleDTO>> getAllUsers() {
        return usersService.getAllUsers();
    }

    /***
     * 获取用户信息
     * @param sessionId sessionId
     * @return 获取用户信息结果
     */
    @ApiOperation(value = "获取用户信息")
    @RequirePermission("user")
    @GetMapping("/getUserInfo")
    Result<UserRoleDTO> getUserInfo(@CookieValue(name = "SESSIONID", required = false) String sessionId) {
        int userId = userUtils.getUserId(sessionId);
        return usersService.getUserInfo((long) userId);
    }

    /***
     * 冻结用户
     * @param userId 用户id
     * @return 冻结用户结果
     */
    @ApiOperation(value = "冻结用户")
    @RequirePermission("admin")
    @PutMapping("/passUser")
    @RateLimiter
    public Result<T> passUser(@RequestParam Long userId) {
        return usersService.passUser(userId);
    }


}
